﻿Imports Microsoft.VisualBasic
Imports System.Text


Public Module SharedRoutines
    Const TRUNCATE_COUNT As Integer = 0


    Public Function Truncate(ByVal originalInput As String) As String
        Return truncate(originalInput, TRUNCATE_COUNT)

    End Function

    Public Function Truncate(ByVal originalInput As String, ByVal wordsLimit As String) As String
        If originalInput IsNot Nothing And originalInput <> "" Then

            Dim output As New StringBuilder(originalInput.Length)
            Dim input As New StringBuilder(originalInput)

            Dim words As Integer = 0
            Dim lastwasWS As Boolean = True
            Dim count As Integer = 0

            Do
                If Char.IsWhiteSpace(input(count)) Then
                    lastwasWS = True
                Else
                    If lastwasWS Then
                        words = words + 1
                    End If
                    lastwasWS = False
                End If

                output.Append(input(count))
                count = count + 1

            Loop While (words < wordsLimit Or lastwasWS = False) And (count < originalInput.Length)
            Return output.ToString
        Else
            Return String.Empty
        End If
    End Function

    Public Function Encode(ByVal contents As String) As String
        contents = HttpUtility.HtmlDecode(Convert.ToString(contents))

        contents = contents.Replace("  ", "&nbsp;&nbsp;").Replace("\t", "&nbsp;&nbsp;&nbsp;").Replace("\n", "<br>")
        Return contents
    End Function
End Module
